package com.aneliya.framework.mvc.model;

import java.util.Hashtable;

public class Model implements IModel {
	
	private static Model instance;
	public static Model getInstance() {
		if (instance == null) {
			instance = new Model();
		}
		return instance;
	}

	private Hashtable<String, Proxy> proxiesRegister;
	
	protected Model() {
		proxiesRegister = new Hashtable<String, Proxy>();
	}

	public void registerProxy(Proxy proxy, String name) throws Exception {
		if (!proxiesRegister.containsKey(name)) {
			if (proxy.name == null) {
				proxy.name = name;
				proxiesRegister.put(name, proxy);
			} else if (proxy.name != name) {
				if (proxiesRegister.containsKey(proxy.name)) {
					throw new Exception("This proxy is already registered under name: " + proxy.name);
				} else {
					proxy.name = name;
					proxiesRegister.put(name, proxy);
				}
			} else {
				proxiesRegister.put(name, proxy);
			}
		} else {
			throw new Exception("There is already a registered proxy for name: " + name);
		}
	}

	public void removeProxy(String name) throws Exception {
		if (proxiesRegister.containsKey(name)) {
			proxiesRegister.remove(name);
		} else {
			throw new Exception("There is no registered proxy for name: " + name);
		}
	}

	public Proxy retrieveProxy(String name) throws Exception {
		if (proxiesRegister.containsKey(name)) {
			return proxiesRegister.get(name);
		} else {
			throw new Exception("There is no registered proxy for name: " + name);
		}
	}

}
